<?php
namespace app\admin\controller;

//规格管理
class Norm extends Common
{
	//标签列表
    public function norm_list()
    {
        $where=array('a.gn_id'=>array('gt',0));
        $name=Request()->param('name');
        //名称
        $gn_name=Request()->param('gn_name');
        //规格名称
        $status=Request()->param('status');
        //状态
        $start_time=strtotime(Request()->param('start_time'));
        $end_time=strtotime(Request()->param('end_time'));
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
        if($name!=''){
            $where['b.g_name']=array('like','%'.$name.'%');
        }
        if($gn_name!=''){
            $where['a.gn_name']=array('like','%'.$gn_name.'%');
        }
        $where['a.gn_status'][]=array('neq',3);
        if($status>0){
            if($status==1){
                $where['a.gn_status'][]=1;
            }else{
                $where['a.gn_status'][]=2;
            }
        }else{
            $where['a.gn_status'][]=array('in','1,2');
        }
        $where_time=array(array('gt',0),array('gt',0));
        if($start_time>0){
            $where_time[]=array('egt',$start_time);
        }else{
            $start_time='';
        }
        if($end_time>0){
            $where_time[]=array('elt',$end_time);
        }else{
            $end_time='';
        }
        $where['a.gn_create_time']=$where_time;
        $norm=db('goods_norm a')
            ->field("a.*,b.g_name")
            ->join('fr_goods b','a.g_id=b.g_id','left')
            ->order('a.gn_sort desc,a.gn_status')
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //标签列表
        foreach($norm as $k=>$v){
            $norm[$k]['gn_details_price']=round($v['gn_price']/$v['gn_details_number'],2).'/'.$v['gn_details_unit'];
        }
        $count=db('goods_norm a')
            ->join('fr_goods b','a.g_id=b.g_id','left')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($norm));//分页
        $this->view->assign('page_str',$page_str);
        $this->view->assign('data',$norm);
        $this->assign('status',$status);
        $this->assign('name',$name);
        $this->assign('gn_name',$gn_name);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();

    }
	//添加标签
	public function add_norm(){
		if(request()->isGet()){
			//get，加载页面
            $id=request()->get('id');
            $goods=db('goods')
                ->where(array('g_id'=>$id))
                ->find();
            $this->assign('goods',$goods);
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();
			$id=$data['id'];
            unset($data['id']);
            $goods=db('goods')
                ->where(array('g_id'=>$id))
                ->find();
            if(empty($goods)){
                return return_json(array('code'=>0,'msg'=>'该商品出错，无法添加规格'),'添加规格');
            }
            $data1=array();
            $is_reduction=0;
            for($i=1;$i<=5;$i++){
                $name=trim($data['gn_name_'.$i]);//名称
                $price=$data['gn_price_'.$i];//售价
                $old_price=$data['gn_old_price_'.$i];//原价
                $weight=$data['gn_weight_'.$i];//总重量
                $stock=$data['gn_stock_'.$i];//库存
                $one_number=$data['gn_one_number_'.$i];//单件说明
                $sort=$data['gn_sort_'.$i];//排序
                $status=$data['gn_status_'.$i];//状态
                $gn_details_number=$data['gn_details_number_'.$i]>1 ? $data['gn_details_number_'.$i]:1;//详细价格数量
                $gn_details_unit=$data['gn_details_unit_'.$i]!='' ? $data['gn_details_unit_'.$i]:'件';//详细价格单位
                if($name!='' && $price > 1 && $weight >0){
                    $data1[]=array('g_id'=>$id,'gn_price'=>$price,'gn_old_price'=>$old_price,'gn_stock'=>$stock,'gn_name'=>$name,'gn_weight'=>$weight,'gn_one_number'=>$one_number,'gn_status'=>$status,'gn_sort'=>$sort,'gn_details_unit'=>$gn_details_unit,'gn_details_number'=>$gn_details_number,'gn_add_user_id'=>session('uid'),'gn_create_time'=>time(),'gn_add_user_name'=>session('user.real_name'));
                }
                if($old_price>0 && $status=1){
                    $is_reduction=1;
                }
            }
            if($is_reduction==1){
                if(!in_array('2',explode(',','0'.$goods['gl_ids'].'0'))){
                    if($goods['gl_ids']!=''){
                        $gl_str=$goods['gl_ids'].'2,';
                    }else{
                        $gl_str=',2,';
                    }
                    db('goods')->where(array('g_id'=>$id))->update(array('gl_ids'=>$gl_str));
                }
            }
            if(count($data1)<1){
                return return_json(array('code'=>0,'msg'=>'请至少上传一个完整的规格'),'添加规格');
            }
            $res=db('goods_norm')->insertAll($data1);
            //添加权限
            if($res!=false){
                return return_json(array('code'=>1,'msg'=>'规格添加成功'),'添加规格');
            }else{
                return return_json(array('code'=>0,'msg'=>'规格添加失败'),'添加规格');
            }
		}
    }
    //修改规格
    function renorm(){
        if(request()->isGet()){
            //get，加载页面
            $id=request()->get('id');
            $norm=db('goods_norm a')
                ->field("a.*,b.g_name")
                ->join("fr_goods b","a.g_id = b.g_id","left")
                ->where(array('a.gn_id'=>$id))
                ->find();
            $this->assign('norm',$norm);
            return $this->view->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $id = $data['id'];
            unset($data['id']);
            $norm=db('goods_norm a')
                ->where(array('a.gn_id'=>$id))
                ->find();
            if(empty($norm)){
                return return_json(array('code'=>0,'msg'=>'该规格出错，无法修改'),'修改规格');
            }
            $goods=db('goods')
                ->where(array('g_id'=>$norm['g_id']))
                ->find();
            if(empty($goods)){
                return return_json(array('code'=>0,'msg'=>'该商品出错，无法修改规格'),'修改规格');
            }
            if($data['gn_price']<1){
                return return_json(array('code'=>1,'msg'=>'售价必须大于1'),'修改规格');
            }
            if($data['gn_stock']<0){
                return return_json(array('code'=>1,'msg'=>'售价必须不少于0'),'修改规格');
            }
//            if($data['gn_details_number']<1){
//                return return_json(array('code'=>1,'msg'=>'详细价格数量必须大于1'),'修改规格');
//            }
//            if($data['gn_details_unit']==''){
//                return return_json(array('code'=>1,'msg'=>'详细价格单位不可为空'),'修改规格');
//            }
            if($data['gn_old_price']>0){
                if($data['gn_old_price']<$data['gn_price']){
                    return return_json(array('code'=>1,'msg'=>'原价必须大于售价'),'修改规格');
                }
                if(!in_array('2',explode(',','0'.$goods['gl_ids'].'0'))){
                    if($goods['gl_ids']!=''){
                        $gl_str=$goods['gl_ids'].'2,';
                    }else{
                        $gl_str=',2,';
                    }
                    db('goods')->where(array('g_id'=>$id))->update(array('gl_ids'=>$gl_str));
                }
            }

            $res=db('goods_norm')->where(array('gn_id'=>$id))->update($data);
            //添加权限
            if($data['gn_price']!=$norm['gn_price']){
                db('goods_log')->insert(array('admin_user_id' => session('uid'), 'admin_user_name' => session('user.real_name'), 'g_id' => $norm['g_id'], 'create_time' => time(), 'action' => '修改规格，id='.$id.',名称='.$norm['gn_name'].'，原价格='.$norm['gn_price'].'，现价格='.$data['gn_price']));
            }
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'规格修改成功'),'修改规格');
            }else{
                return return_json(array('code'=>0,'msg'=>'规格修改失败'),'修改规格');
            }

        }
    }
    //规格禁用
    function norm_del(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('goods_norm')
                ->where(array('gn_id'=>$id,'gn_status'=>array('neq',3)))
                ->update(array('gn_status'=>2,'gn_update_time'=>time()));
            if($res){
                return return_json(array('code'=>1,'msg'=>'禁用成功！'),'禁用规格');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败！'),'禁用规格');die;
            }
        }
    }
    //规格启用
    function norm_use(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('goods_norm')
                ->where(array('gn_id'=>$id,'gn_status'=>array('neq',3)))
                ->update(array('gn_status'=>1,'gn_update_time'=>time()));
            if($res){
                return return_json(array('code'=>1,'msg'=>'启用成功！'),'启用规格');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败！'),'启用规格');die;
            }
        }
    }
    //规格删除
    function norm_real_del(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('goods_norm')
                ->where(array('gn_id'=>$id))
                ->update(array('gn_status'=>3,'gn_update_time'=>time()));
            if($res){
                return return_json(array('code'=>1,'msg'=>'删除成功！'),'删除规格');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'删除失败！'),'删除规格');die;
            }
        }
    }
}
